<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>用户登录</h2>
        <p class="subtitle">请输入您的账号信息</p>
      </div>
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="goToRegister">没有账号？去注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 450px;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #409EFF;
  margin-bottom: 10px;
}

.subtitle {
  color: #909399;
}
</style>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onSubmit() {
      const storedUser = this.$store.state.user;
      
      if(!this.form.username || !this.form.password) {
        this.$message.error('请输入用户名和密码');
        return;
      }
      
      if(this.form.username === storedUser.username && 
         this.form.password === storedUser.password) {
        // 存储用户信息到localStorage
        localStorage.setItem('user', JSON.stringify({
          username: this.form.username,
          password: this.form.password
        }));
        
        this.$message.success('登录成功');
        
        // 如果有重定向地址，则跳转到重定向地址
        const redirect = this.$route.query.redirect || '/dashboard';
        this.$router.push(redirect);
      } else {
        this.$message.error('用户名或密码错误');
      }
    },
    goToRegister() {
      this.$router.push('/register');
    }
  }
}
</script>